
var add_dialog = null;

var createAddDialog = function(){
    
    // 表单项创建
    var formItems = [
	         {
	             fieldLabel : "优惠名",
	             name : "name",
	             emptyText : "优惠的名称是什么？",
	             allowBlank : false,
	             blankText : "优惠不能没有名字"
	         },
	         {
	             xtype: 'radiogroup',
	             fieldLabel: '类型',
	             name : "type",
	             defaultType: "radiofield",
	             listeners : {
	        	 change : function(view, newValue) {
	        	     if (newValue.type == 2) {
	        		view.nextNode("numberfield").setVisible(true); 
	        	     } else {
	        		view.nextNode("numberfield").setVisible(false); 
	        	     }
	        	 }
	             },
	             items: [
	                 {boxLabel: '全城热推', name: 'type', inputValue: 1, checked: true},
	                 {boxLabel: '路过惊喜', name: 'type', inputValue: 2},
	             ]
	         },
	         {
	             xtype : "numberfield",
	             name : "area",
	             minValue : 500,
	             value : 1000,
	             maxValue : 10000,
	             fieldLabel : "影响区域(米)",
	             hidden : true
	         },
	         {
	             xtype : "datefield",
	             fieldLabel : "开始时间",
	             name : "beg_time",
	             id : "beg_time",
	             value : new Date(),
	             allowBlank : false,
	             blankText : "开始时间不得为空",
	             format : "Y-m-d",
	             invalidText : "格式为 '年-月-日'"
	         },
	         {
	             xtype : "datefield",
	             fieldLabel : "结束时间",
	             name : "end_time",
	             id : "end_time",
	             value : new Date(),
	             allowBlank : false,
	             blankText : "结束时间不得为空",
	             format : "Y-m-d",
	             invalidText : "格式为 '年-月-日'"
	         },
	         {
	             xtype : "filefield",
	             fieldLabel : "优惠图片",
	             allowBlank : false,
	             blankText : "优惠不能没有图片",
	             name : "image",
	             id : "select_image",
	             buttonText : "选择图片",
	             autoRender: 'imagePrev'
	         },{
	             xtype : "textareafield",
	             emptyText : "优惠的有什么具体内容？",
	             grow : true,
	             allowBlank : false,
	             blankText : "优惠内容不能为空",
	             name : "context",
	             fieldLabel : "优惠内容"
	         }, {
	             xtype : "textfield",
	             value : "优惠添加失败",
	             readOnly : true,
	             border : false,
	             itemId : "fail_info",
	             hidden : true
	         }
	];
    
    // 表单按钮
    var formButtons = [
	           {
	               text : "确定",
	               handler : function() {
	        	   if (form_panel.getForm().isValid()) {
    	        	   form_panel.getForm().submit({
    	        	       url: "/QuickLifeServer/WebCouponManageAction?actionflag=addcp&busid=" + Ext.getDom("id").value,
    	        	       method: "POST",
    	        	       waitTitle:"请稍候",
    	        	       waitMsg:"正在上传优惠信息。。。",
    	        	       success: function(form, action) {
    	        		   var rt = JSON.parse(action.response.responseText);
    	        		   if (rt.result == "SUCCESS") {
    	        		       add_dialog.close();
    	        		   } else {
    	        		       Ext.ComponentQuery.query("#fail_info")[0].setValue(rt.rtinfos).setVisible(true);
    	        		   }
    	        	       },
    	        	       failure: function(form, action) {
    	        		   var rt = JSON.parse(action.response.responseText);
 	        		   if (rt.result == "SUCCESS") {
 	        		       add_dialog.close();
 	        		   } else {
 	        		       Ext.ComponentQuery.query("#fail_info")[0].setValue(rt.rtinfos).setVisible(true);
 	        		   }
    	        	       }
    	        	   });
    	               }
	               }
	           }
	        ];
    
    // 实例化表单
    var form_panel = Ext.create("Ext.form.Panel", {
	autoScroll: true,
	url: "/QuickLifeServer/WebCouponManageAction",
	method: "POST",
	layout : {
	   type : 'vbox',
	   pack : "Even",
	   align : "center"
	},
	renderTo : Ext.getBody(),
	defaultType : 'textfield',
	defaults : {
	    width : "60%",
	    style : {
		marginTop : "10px"
	    }
	},
	items : formItems
    });
    
    // 弹出窗实例化
    add_dialog = Ext.create('Ext.window.Window', {
	title : "添加优惠",
	modal: true,
	height : 500, 
	width : 500,
	layout : "fit",
	shadow : "drop",
	shadowOffset : 30,
	items : form_panel,
	autoScroll: true,
	buttons: formButtons
    });
    // 显示弹出窗
    add_dialog.show();
};

function add_coupons() {
    createAddDialog();
}
